Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Fix Eager loading partially nullable morphTo relations #36129

Merged
merged 3 commits into from
Feb 8, 2021

Conversation

jan-demsar
Copy link
Contributor

This pull request fixes errors when 1 model has multiple polymorphic relationships sharing same "type" column.

In case we have a polymorphic relationship, where _type column is empty, script will work great. But if the _id column is empty, script will throw an exception.

For an example, if model has the following columns:

  • related_type (non-nullable)
  • related_old_id (nullable)
  • related_new_id (nullable)

and the following relationships:

    public function oldRelatedModel(): MorphTo
    {
        return $this->morphTo(name: null, type: 'related_type', id: 'related_old_id');
    }

    public function newRelatedModel(): MorphTo
    {
        return $this->morphTo(name: null, type: 'related_type', id: 'related_new_id');
    }

When we eager load both polymorphic relationships and one of the columns (related_old_id or related_new_id) are null, execution will fail (especially if we are dealing with uuids) because the function will convert all values to string (null => '') and '' is invalid uuid syntax.

ERROR: invalid input syntax for type uuid: \"\" (SQL: select * from \"user\" where \"user\".\"id\" in (, 68504945-959d-4e13-b1b4-7a250523cc24))"

This pull requests filters out nullable values and eager loads only non-nullable values.

@taylorotwell
Copy link
Member

taylorotwell commented Feb 4, 2021

Do you really need the callback at all? Doesn't array_filter with no callback already remove empty items?

@jan-demsar
Copy link
Contributor Author

Do you really need the callback at all? Doesn't array_filter with no callback already remove empty items?

You are right, it is not needed. I have removed it.

@taylorotwell taylorotwell merged commit 79b98fe into laravel:8.x Feb 8, 2021
abdallhsamy added a commit to abdallhsamy/framework that referenced this pull request Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants